/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include <map>
#include "json/json.h"

#define DEP_FILE    "Dependency.json"
#define CMD_GIT     "git clone %s %s"
#define DEFAULT_DEP_REPO_PATH   "https://git.oschina.net/lingzimaike/"

#ifdef _WINDOWS
#define DEP_PLATFORM    "windows"
#endif

int executeCmd(const char* _Cmd, ...);
bool parseFile(const char* _File, Json::Value& _Root);
int doWork(const std::string& _DepDir, const std::string& _DepName, 
    const std::string& _DepVer, const std::string& _DepRepo)
{
    std::string _FullDepRepo;
    if (!_DepRepo.empty()) {
        _FullDepRepo = _DepRepo;
    } else {
        _FullDepRepo.append(DEFAULT_DEP_REPO_PATH);
        _FullDepRepo.append(_DepName);
        _FullDepRepo.append(".");
        _FullDepRepo.append(_DepVer);
        _FullDepRepo.append(".git");
    }
    std::string _DepTargetPath = _DepDir;
    _DepTargetPath.append(_DepName);

    executeCmd(CMD_GIT, _FullDepRepo.c_str(), _DepTargetPath.c_str());

    return 0;
}


int main(int _Argc, char* _Argv[])
{
    Json::Value _Root;
    if (!parseFile(DEP_FILE, _Root)) {
        perror("parse dependency file: ");
        return -1;
    }

    std::map<std::string, int> _LoadedDeps;

    std::string _DependencyDirectory("Dependency/");
    if (!_Root["DependencyDirectory"].isNull()) {
        _DependencyDirectory = _Root["DependencyDirectory"].asString();
    }

    Json::Value _Dependencies;
    if (!_Root["Dependencies"].isNull()) {
        _Dependencies = _Root["Dependencies"];
        
        if (_Dependencies.isNull()) {
            return 0;
        }

        Json::Value::ArrayIndex _DepCount = _Dependencies.size();
        for (Json::Value::ArrayIndex _Index = 0; _Index < _DepCount; ++_Index)
        {
            Json::Value _Dep = _Dependencies[_Index];

            std::string _DepName = _Dep["name"].asString();
            std::string _DepVersion = _Dep["version"].asString();
            std::string _DepRepo;
            std::string _DepPlatform;
            if (!_Dep["repo"].isNull()) {
                _DepRepo = _Dep["repo"].asString();
            }
            if (!_Dep["platform"].isNull()) {
                _DepPlatform = _Dep["platform"].asString();

                if (_DepPlatform.compare(DEP_PLATFORM) != 0) {
                    continue;
                }
            }

            if (_LoadedDeps.find(_DepName) != _LoadedDeps.end()) {
                continue;
            }
            doWork(_DependencyDirectory, _DepName, _DepVersion, _DepRepo);
            _LoadedDeps[_DepName] = 1;
        }
    }

    return 0;
}